home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / pctim12.zip / TCINT8.C < prev    next >
C/C++ Source or Header  |  1995-01-29  |  4KB  |  176 lines

  1. #include <dos.h>
  2. #include <bios.h>
  3. #include <conio.h>
  4. #include "tcint8.h"
  5.  
  6. #define IRQ0 0x8
  7. #define PIT0 0x40
  8. #define PIT1 0x41
  9. #define PIT2 0x42
  10. #define PITMODE 0x43
  11. #define PITCONST 1193180L
  12. #define PIT0DEF 18.2067597
  13. #define KBCTRL 0x61
  14. #define NEW8H 1
  15.  
  16. static float    tick_per_ms = 0.0182068;
  17. static float    ms_per_tick = 54.9246551;
  18. static float    freq8h = 18.2067597;
  19. static unsigned char flag8h = 0;
  20. static void     interrupt(*old8h) (void);
  21.  
  22. volatile int    counter_8h;
  23. volatile int    counter_reset;
  24. volatile unsigned long int ticks_8h;
  25.  
  26. void            init8h(unsigned int Hz)
  27. {
  28.     unsigned int    pit0_set,
  29.                     pit0_value;
  30.  
  31.     if (flag8h != NEW8H) {
  32.         disable();
  33.         old8h = getvect(IRQ0);
  34.         setvect(IRQ0, new8h);
  35.         outportb(PITMODE, 0x36);
  36.         pit0_value = PITCONST / Hz;
  37.         pit0_set = (pit0_value & 0x00ff);
  38.         outportb(PIT0, pit0_set);
  39.         pit0_set = (pit0_value >> 8);
  40.         outportb(PIT0, pit0_set);
  41.         enable();
  42.  
  43.         flag8h = NEW8H;
  44.         freq8h = Hz;
  45.         counter_8h = 0;
  46.         counter_reset = freq8h / PIT0DEF;
  47.         tick_per_ms = freq8h / 1000;
  48.         ms_per_tick = 1000 / freq8h;
  49.     }
  50. }
  51.  
  52. void            quit8h(void)
  53. {
  54.     unsigned int    pit0_set,
  55.                     pit0_value;
  56.     long            tick;
  57.     char           *cmostime;
  58.  
  59.     if (flag8h == NEW8H) {
  60.         disable();
  61.         outportb(PITMODE, 0x36);
  62.         outportb(PIT0, 0x00);
  63.         outportb(PIT0, 0x00);
  64.         setvect(IRQ0, old8h);
  65.         enable();
  66.  
  67.         cmostime = get_cmostime();
  68.         tick = PIT0DEF *
  69.             (
  70.              (((float) *cmostime) * 3600) +
  71.              (((float) *(cmostime + 1)) * 60) +
  72.              (((float) *(cmostime + 2)))
  73.             );
  74.         biostime(1, tick);
  75.  
  76.         flag8h = 0;
  77.         freq8h = PIT0DEF;
  78.         counter_reset = freq8h / PIT0DEF;
  79.         tick_per_ms = freq8h / 1000;
  80.         ms_per_tick = 1000 / freq8h;
  81.     }
  82. }
  83.  
  84. void            interrupt
  85.                 new8h(void)
  86. {
  87.     ticks_8h++;
  88.     counter_8h++;
  89.  
  90.     if (counter_8h == counter_reset) {
  91.         counter_8h = 0;
  92.         old8h();
  93.     } else {
  94.         disable();
  95.         outportb(0x20, 0x20);
  96.     }
  97. }
  98.  
  99. unsigned long   time8h(unsigned long start, unsigned long stop)
  100. {
  101.     unsigned long   duration,
  102.                     millisec;
  103.  
  104.     if (stop < start)
  105.         return 0;
  106.     else {
  107.         duration = stop - start;
  108.         millisec = duration * ms_per_tick;
  109.         return millisec;
  110.     }
  111. }
  112.  
  113. void            delay8h(unsigned int delayms)
  114. {
  115.     unsigned long int delaybegin = 0;
  116.     unsigned long int delayend = 0;
  117.     unsigned int    delaytick;
  118.  
  119.     delaytick = delayms * tick_per_ms;
  120.  
  121.     if (flag8h == NEW8H)
  122.         delaybegin = ticks_8h;
  123.     else
  124.         biostime(0, (long) delaybegin);
  125.  
  126.     do {
  127.         if (flag8h == NEW8H)
  128.             delayend = ticks_8h;
  129.         else
  130.             biostime(0, (long) delayend);
  131.     } while ((delayend - delaybegin) < delaytick);
  132. }
  133.  
  134. void            sound8h(int freq, int duration)
  135. {
  136.     int             byte;
  137.     unsigned int    freq1;
  138.  
  139.     freq1 = PITCONST / freq;
  140.     outportb(PITMODE, 0xb6);
  141.     byte = (freq1 & 0xff);
  142.     outportb(PIT2, byte);
  143.     byte = (freq1 >> 8);
  144.     outportb(PIT2, byte);
  145.     byte = inportb(KBCTRL);
  146.     outportb(KBCTRL, (byte | 3));
  147.  
  148.     delay8h(duration);
  149.     outportb(KBCTRL, (byte & 0xfc));
  150. }
  151.  
  152. char           *get_cmostime(void)
  153. {
  154.     union REGS      inreg;
  155.     union REGS      outreg;
  156.     char           *buff;
  157.     static char     buffer[6];
  158.     char            ch;
  159.  
  160.     buff = buffer;
  161.     inreg.h.ah = 0x02;
  162.     int86(0x1a, &inreg, &outreg);
  163.  
  164.     ch = outreg.h.ch;
  165.     buffer[0] = (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * 10);
  166.     ch = outreg.h.cl;
  167.     buffer[1] = (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * 10);
  168.     ch = outreg.h.dh;
  169.     buffer[2] = (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * 10);
  170.     buffer[3] = outreg.h.dl;
  171.     buffer[4] = (char) (outreg.x.cflag & 0x0001);
  172.     buffer[5] = 0x00;
  173.  
  174.     return (buff);
  175. }
  176.